<?php 
    class Tk_accounts_subject_model extends CI_Model{

        function __construct(){
            parent::__construct();
        }

        function get_subject_by_id($id){
            $sql = "SELECT * FROM {$this->db->dbprefix}tk_accounts_subject where id={$id}";
            return $this->db->query($sql)->row_array();
             
        }

        function get_subject_list_by_pid($pid){
            $sql = "SELECT s.id,s.pid,s.title, count(s1.id) AS children FROM {$this->db->dbprefix}tk_accounts_subject AS s ";
            $sql .= "LEFT JOIN {$this->db->dbprefix}tk_accounts_subject AS s1 ON(s1.pid=s.id) ";
            $sql .= " where s.status = 1 AND s.pid = $pid GROUP BY s.id";
            $result = $this->db->query($sql)->result_array();
            return $result;
        }


        function get_top_subject_list(){
            $sql = "SELECT * FROM {$this->db->dbprefix}tk_accounts_subject where pid=0";
            $result = $this->db->query($sql)->result_array();
            return $result;
        }

        function get_subject_list($pid){
            $sql = "SELECT * FROM {$this->db->dbprefix}tk_accounts_subject where pid={$pid}";
            $result = $this->db->query($sql)->result_array();
            return $result;
        }

        function add($data){
            $data['add_time'] = time();
            return $this->db->insert('tk_accounts_subject',$data);
        }
        function edit($data){
            $id = $data['id'];
            unset($data['id']);
            $data['add_time'] = time();
            return $this->db->update('tk_accounts_subject',$data, array('id' => $id));
        }

        function delete($id){
            return $this->db->delete('tk_accounts_subject', array('id' => $id ));
        }
    }